package com.blb.examsystem.controller;
import com.blb.common.controller.BaseServlet;
import com.blb.common.entity.PageBean;
import com.blb.common.entity.Question;
import com.blb.common.entity.VO.QuestionVO;
import com.blb.common.utils.SpringUtils;
import com.blb.examsystem.service.QuestionService;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

@Controller
public class QuestionServlet {
    @Autowired
    private QuestionService questionService ;

    private Question studentVoToStudent(QuestionVO questionVO,String image){
        Question question = new Question();
        question.setId(questionVO.getId());
        question.setContent(questionVO.getContent());
        question.setOptionA(questionVO.getOptionA());
        question.setOptionB(questionVO.getOptionB());
        question.setOptionC(questionVO.getOptionC());
        question.setOptionD(questionVO.getOptionD());
        question.setAnswer(questionVO.getAnswer());
        question.setDifficulty(questionVO.getDifficulty());
        question.setQuestionSetId(questionVO.getQuestionSetId());
        question.setImage(image);
        return question;
    }

    //上传目录
    public static final String UPLOAD_DIR = "F:\\image\\";
    @RequestMapping("/findPage")
    public String findPage(@RequestParam(value = "current",required = false,defaultValue = "1") Integer current, Model model) throws ServletException, IOException {
        PageBean<Question> page = questionService.findPage(current);
        model.addAttribute("page",page);
        return "questions";
    }

    //按标题查找
    @RequestMapping("/searchByContent")
    public String searchByContent(String content,Model model,@RequestParam(value = "current",required = false,defaultValue = "1") Integer current) throws ServletException, IOException {
        //获取参数
        PageBean<Question> pageByContent = questionService.findPageByContent(content, current);
        model.addAttribute("page",pageByContent);
        return "questions";

    }

    //添加
    @RequestMapping("/addQuestion1")
    public String addQuestion(QuestionVO questionVO , MultipartFile image) throws ServletException, IOException {
        //处理文件名
        String fileName = image.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID().toString().replace("-","") + suffix;
        //保存文件
        image.transferTo(new File(UPLOAD_DIR + fileName));
        //添加到数据库 StudentVO --> Student BeanUtils
        questionService.addQuestion(studentVoToStudent(questionVO,fileName));
        return "redirect:/findPage";
    }

    //按id查找
    @RequestMapping("/findQuestionById")
    public String findQuestionById(Integer id ,Model model) throws ServletException, IOException {
        Question question = questionService.findById(id);
        model.addAttribute("question",question);
        return "updateQuestion";
    }

    //更新
    @RequestMapping("/modifyQuestion")
    public String modifyQuestion(QuestionVO questionVO , MultipartFile image) throws ServletException, IOException {
        //处理文件名
        String fileName = image.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID().toString().replace("-","") + suffix;
        //保存文件
        image.transferTo(new File(UPLOAD_DIR + fileName));
        //添加到数据库 StudentVO --> Student BeanUtils
        questionService.addQuestion(studentVoToStudent(questionVO,fileName));
        return "redirect:/findPage";
    }

    //删除
    @RequestMapping("/removeQuestion")
    public String removeQuestion(Integer id) throws ServletException, IOException {
        questionService.removeQuestion(id);
        return "redirect:/findPage";
    }

}
